package cn.com.cfae.iras.doc.analyze.components.integrity;

import cn.com.cfae.iras.doc.analyze.beans.web.Detail;
import cn.com.cfae.iras.doc.analyze.components.BaseComponent;
import cn.com.cfae.iras.doc.analyze.components.beans.ComponentResult;
import cn.com.cfae.iras.doc.analyze.handUtil.HandingData;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.model.ChapterModel;
import cn.com.cfae.iras.doc.analyze.parse.model.DocumentModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 架构完整性
 *
 * 是否同时含有"注册名称（发行人）"，"法定代表人"，"注册资本"，"设立（工商注册）日期"，"工商登记号"，
 * "住所（注册地址）"，"邮政编码"，"电话"，"传真号码"。
 *
 * 预评/发行阶段
 */
@Component("I1016")
@Scope("prototype")
public class I1016 extends BaseComponent {
    private static Logger log = LoggerFactory.getLogger(I1016.class);

    @Autowired
    private HandingData handingData;
    @Override
    public List<ComponentResult> handlerM() {
        log.info("获取I1016组件M");
        List<ComponentResult> componentResults = checkRulesI1016(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBM() {
        return null;
    }

    @Override
    public List<ComponentResult> handlerDM() {
        log.info("获取I1016组件DM");
        List<ComponentResult> componentResults = checkRulesI1016(getCatalogId(),getCatalogId(),getChapterId(),getRuleTitle());
        return componentResults;
    }

    @Override
    public List<ComponentResult> handlerBDM() {
        return null;
    }


    private List<ComponentResult> checkRulesI1016(String model,String catalogId,String chapterId,String ruleName){
        DocumentModel extractData = super.getExtractData();
        List<ComponentResult> componentResults = new ArrayList<>();
        ComponentResult componentResult = null;
        ExtractItem extractItem = null;
        int pageNum = 0;
        List<ComponentResult.TextPosition> contentAndPositionList = null;

        String [] keywords = new String[]{"注册名称","法定代表人","注册资本","设立(工商注册)日期","工商登记号","住所","邮政编码","电话","传真号码"};
        boolean flag = false;
        int startPage = 0;
        try {

            if (extractData!= null){
                ChapterModel chapterModel = extractData.getChapterModel(model);
                startPage = chapterModel!=null?chapterModel.getStartPage():0;
                //发行人、注册名称
                extractItem = getExtractItem(extractData,catalogId,"FXR","FXRMC");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String keyText = key!=null?key.getText():null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    if (StringUtils.isNotEmpty(keyText)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-注册名称", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-注册名称", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-注册名称", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }
                //法定代表人
                extractItem = getExtractItem(extractData,catalogId,"FXR","FDDBR");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String ktext = key!=null?key.getText():null;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    if (StringUtils.isNotEmpty(ktext)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-法定代表人", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName, "关键词检查-法定代表人", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-法定代表人", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }
                //注册资本
                extractItem = getExtractItem(extractData,catalogId,"FXR","ZCZB");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String ktext = key!=null?key.getText():null;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    if (StringUtils.isNotEmpty(ktext)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-注册资本", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-注册资本", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-注册资本", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }
                //设立（工商注册）日期
                extractItem = getExtractItem(extractData,catalogId,"FXR","SLRQ");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String ktext = key!=null?key.getText():null;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    if (StringUtils.isNotEmpty(ktext)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-设立（工商注册）日期", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-设立（工商注册）日期", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-设立（工商注册）日期", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }

                //工商登记号
                extractItem = getExtractItem(extractData,catalogId,"FXR","GSDJH");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String ktext = key!=null?key.getText():null;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    if (StringUtils.isNotEmpty(ktext)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-工商登记号", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName, "关键词检查-工商登记号", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId,ruleName, "关键词检查-工商登记号", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }

                //注册地址(住所)
                extractItem = getExtractItem(extractData,catalogId,"FXR","LXDZ");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String ktext = key!=null?key.getText():null;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    if (StringUtils.isNotEmpty(ktext)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-住所", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-住所", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-住所", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }

                //邮政编码
                extractItem = getExtractItem(extractData,catalogId,"FXR","YZBM");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String ktext = key!=null?key.getText():null;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    if (StringUtils.isNotEmpty(ktext)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-邮政编码", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-邮政编码", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-邮政编码", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }

                //电话
                extractItem = getExtractItem(extractData,catalogId,"FXR","DH");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String ktext = key!=null?key.getText():null;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    if (StringUtils.isNotEmpty(ktext)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-电话", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-电话", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }
                else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-电话", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }
                //传真号码
                extractItem = getExtractItem(extractData,catalogId,"FXR","CZ");
                if (extractItem!=null){
                    KVText key = extractItem.getKey();
                    String ktext = key!=null?key.getText():null;
                    contentAndPositionList = key!=null?getContentAndPositionList(key):null;
                    pageNum = key!=null?key.getPageNumber():startPage;
                    pageNum = pageNum!=0?pageNum:startPage;
                    if (StringUtils.isNotEmpty(ktext)){
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-传真号码", "正确", false, getRegulationId(), pageNum, contentAndPositionList, "01");
                    }else {
                        componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-传真号码", "信息披露不完整", true, getRegulationId(), pageNum, contentAndPositionList, "02");
                    }
                    componentResults.add(componentResult);
                }
                else {
                    componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-传真号码", "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                    componentResults.add(componentResult);
                }
            }
        }catch (Exception e){
            componentResults.clear();
            for (String key: keywords){
                componentResult = handingData.getComponentResult(catalogId,chapterId, ruleName, "关键词检查-"+key, "未识别,建议人工核对", true, getRegulationId(), startPage, contentAndPositionList, "03");
                componentResults.add(componentResult);
            }
             log.error(e.getMessage(),e);
        }
        return  componentResults;
    }

    private ExtractItem getExtractItem(DocumentModel extractData, String chapter, String model, String key){
        ExtractItem extractItem  = null;
        if (extractData!=null){
            ChapterModel chapterModel = extractData.getChapterModel(chapter);
            if (chapterModel != null){
                ExtractBizModel extractMode = chapterModel.getModel(model);
                if (extractMode!=null){
                    extractItem = extractMode.getExtractItem(key);
                }
            }
        }
        return extractItem;
    }


}
